package leetcode;

import com.sun.org.apache.bcel.internal.generic.ARETURN;

import java.util.Stack;

/**
 * @author cino
 * justCode
 */
public class leetcode20 {
    public static void main(String[] args) {
        System.out.println(isValid("()[]"));
    }

    public static boolean isValid(String s){
        if (s.length() == 0){
            return true;
        }

        Stack<Character> stack = new Stack<>();
        for (char ch:s.toCharArray()){
            if (ch=='('||ch=='{'||ch=='['){
                stack.push(ch);
            }else {
                if (stack.isEmpty()){
                    return false;
                }else {
                    char temp = stack.pop();
                    if (ch==')'){
                        if (temp!='('){
                            return false;
                        }
                    }else if (ch=='{'){
                        if (temp !='{'){
                            return false;
                        }
                    }else if (ch==']'){
                        if (temp!='['){
                            return false;
                        }
                    }
                }
            }
        }
        return stack.isEmpty()?true:false;
    }
}
